package challenge

import (
	"math/rand"
	"time"

	. "7qule.com/sd3/CrossServer/src/bll/challenge/bll"
	. "7qule.com/sd3/CrossServer/src/bll/player/bll"
	"7qule.com/sd3/CrossServer/src/webServer"
	"7qule.com/sd3/rpcService/Common"
)

func init() {
	webServer.RegisterFunction(new(ChallengeApi))
}

// 玩家请求接口模块
type ChallengeApi struct {
}

//
// GetChallenge
// @Description:进入擂台比武
// @receiver this
// @param playerId:用户id
// @return *webServer.ResponseObject:响应对象
//
func (this *ChallengeApi) GetChallenge(playerId string) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	player, playerExist := GlobalPlayerBll.GetItem(playerId)
	if !playerExist {
		return responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
	}

	// 获取玩家擂台数据
	playerChallenge, exist := GlobalChallengeBll.GetChallengePlayer(player.Id)
	if !exist {
		// 添加到擂台末尾
		playerChallenge = GlobalChallengeStageBll.InitAndAddChallengeStageLast(player.Id)
	}
	challengeStageData := GlobalChallengeStageBll.GetPlayerChallengeStage(playerChallenge.StageId, playerChallenge.Rank)

	responseObj.Data = challengeStageData
	responseObj.SetResultStatus(Common.ResultStatus_Success)
	return responseObj
}

//
// GetChallengeLeaderboard
// @Description: 查看排行榜
// @receiver this
// @return *webServer.ResponseObject:响应对象
//
func (this *ChallengeApi) GetChallengeLeaderboard(playerId string) (responseObj *webServer.ResponseObject) {
	//获取相应信息
	responseObj = webServer.GetInitResponseObj()

	player, playerExist := GlobalPlayerBll.GetItem(playerId)
	if !playerExist {
		responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
		return
	}

	challengePlayer, exist := GlobalChallengeBll.GetChallengePlayer(player.Id)
	if !exist {
		responseObj.SetResultStatus(Common.ResultStatus_PvpRankNotExist)
		return
	}

	challengeLeaderboard := GlobalChallengeLeaderboardBll.GetLeaderboardData()

	data := GlobalChallengeBll.AssembleToClient(challengePlayer.PlayerId)

	// 排行榜数据
	data["challengeLeaderboard"] = challengeLeaderboard

	responseObj.Data = data
	responseObj.SetResultStatus(Common.ResultStatus_Success)
	return

}

//
// Fight
// @Description:挑战
// @receiver this
// @param playerId:用户id
// @param ChallengeId:挑战者索引
// @return *webServer.ResponseObject:响应对象
//
func (this *ChallengeApi) Fight(playerId string, challengedRank int) (responseObj *webServer.ResponseObject) {
	//获取相应信息
	responseObj = webServer.GetInitResponseObj()

	player, playerExist := GlobalPlayerBll.GetItem(playerId)
	if !playerExist {
		responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
		return
	}

	// 获取玩家擂台数据
	playerChallenge, exist := GlobalChallengeBll.GetChallengePlayer(player.Id)
	if !exist {
		responseObj.SetResultStatus(Common.ResultStatus_PvpRankNotExist)
		return
	}

	// 被挑战者
	challengedPlayer := GlobalChallengeStageBll.GetChallengePlayerByRank(playerChallenge.StageId, challengedRank)
	if challengedPlayer == nil {
		responseObj.SetResultStatus(Common.ResultStatus_PvpRankNotExist)
		return
	}

	if playerChallenge.PlayerId == challengedPlayer.PlayerId {
		responseObj.SetResultStatus(Common.ResultStatus_PvpTargetIsOwn)
		return
	}

	//获取锁
	if challengedPlayer.TryLock(playerId) == false {
		responseObj.SetResultStatus(Common.ResultStatus_Cross_TheOtherPlayerFighting)
		return
	}

	rand.Seed(time.Now().Unix())
	randNum := rand.Intn(100)

	//释放锁
	challengedPlayer.TryUnLock()

	if randNum > 50 {
		responseObj.SetResultStatus(Common.ResultStatus_FightFail)
		return
	}

	if playerChallenge.Rank < challengedPlayer.Rank {
		responseObj.SetResultStatus(Common.ResultStatus_Success)
		return
	}
	playerChallengeRank := playerChallenge.Rank
	playerChallengeStageId := playerChallenge.StageId
	challengedPlayerRank := challengedPlayer.Rank

	// 升段
	if challengedRank <= BaseChallengeStageLenModelBll.GetData() && playerChallenge.StageId != BaseChallengeStageModelBll.GetMaxStage() {
		// 玩家升段到下一页签，排名为最后一名
		newStageId := playerChallenge.StageId + 1
		rank := GlobalChallengeStageBll.GetStageLen(newStageId)
		GlobalChallengeBll.UpdateChallengePlayer(playerChallenge.PlayerId, newStageId, rank+1, playerChallenge.IsRobot)
		GlobalChallengeStageBll.AddChallengeStageLast(newStageId, playerChallenge)

		// 之前的页签修改该玩家为机器人
		playerChallengeRobot := GlobalChallengeBll.UpdateChallengePlayerToRobot(playerChallenge.PlayerId, playerChallengeStageId, playerChallengeRank)
		GlobalChallengeStageBll.UpdateChallengePlayer(playerChallengeStageId, playerChallengeRank, playerChallengeRobot)
		// 排行榜处也变成机器人
		GlobalChallengeLeaderboardBll.UpdatePlayer(playerChallengeStageId, playerChallengeRank, playerChallengeRobot)

		responseObj.SetResultStatus(Common.ResultStatus_Success)
		return
	}

	// 未升段，交换排名
	GlobalChallengeBll.UpdateChallengePlayer(playerChallenge.PlayerId, playerChallenge.StageId, challengedPlayer.Rank, playerChallenge.IsRobot)
	GlobalChallengeBll.UpdateChallengePlayer(challengedPlayer.PlayerId, challengedPlayer.StageId, playerChallengeRank, challengedPlayer.IsRobot)
	GlobalChallengeStageBll.ExchangeChallengePlayer(playerChallenge.StageId, playerChallengeRank, challengedPlayerRank)

	// 未升段,被挑战者在排行榜，自己不在排行榜
	LeaderboardLen := BaseChallengeLeaderboardLenModelBll.GetData()
	if challengedRank <= LeaderboardLen && playerChallengeRank > LeaderboardLen {
		// 修改排行榜被挑战者数据为自己
		GlobalChallengeLeaderboardBll.UpdatePlayer(playerChallenge.StageId, challengedPlayerRank, playerChallenge)
		// 未升段,被挑战者和自己都在排行榜
	} else if challengedRank <= LeaderboardLen && playerChallengeRank <= LeaderboardLen {
		// 交换排行榜数据
		GlobalChallengeLeaderboardBll.ExchangeChallengePlayer(playerChallenge.StageId, playerChallengeRank, challengedPlayerRank)
	}
	responseObj.SetResultStatus(Common.ResultStatus_Success)

	return
}
